package com.shopping.mall.core.module.user;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.shopping.mall.core.module.user.entity.User;
import com.shopping.mall.core.module.user.entity.UserExcelVo;
import com.shopping.mall.core.module.user.mapper.UserMapper;
import com.shopping.mall.core.module.user.service.UserExcelService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @Author ZZY
 * @Date 2021/12/8
 */
public class SubjectExcelListener extends AnalysisEventListener<UserExcelVo> {

    private UserMapper userMapper;
    private ArrayList<User> list=new ArrayList<>();

    public SubjectExcelListener(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    //读取excel中的内容，一行一行的读取
    @Override
    public void invoke(UserExcelVo userExcelVo, AnalysisContext analysisContext) {
        if (userExcelVo == null) {
            throw new RuntimeException("文件数据为空");
        }
        System.out.println(userExcelVo);
        list.add(new User(userExcelVo));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        final int i = userMapper.batchInsert(list);
    }
}